using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Belmost4PDA.DatabaseWorkPDA;
using Belmost4PDA.DataRows;

namespace QueryToXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public List<string> columns = new List<string>();
        public List<DataRows> AllRows = new List<DataRows>();

        private void SetupControls()
        {
            AllColumns.Items.Clear();
            Grid.Items.Clear();
            Grid.Columns.Clear();
            foreach (string s in columns)
            {
                AllColumns.Items.Add(s);
                Grid.Columns.Add(s);
            }
            foreach (DataRows row in AllRows)
            {
                bool first = true;
                ListViewItem item = null;
                foreach (string column in columns)
                {
                    if (first)
                    {
                        first = false;
                        try
                        {
                            item = Grid.Items.Add(row.FieldByName(column));
                            item.Tag = row;
                        }
                        catch (ArgumentException)
                        {
                            item = Grid.Items.Add("<No data>");
                            item.Tag = row;
                        }
                    }
                    else
                    {
                        try
                        {
                            item.SubItems.Add(row.FieldByName(column));
                        }
                        catch (ArgumentException)
                        {
                            item.SubItems.Add("<No data>");
                        }
                    }
                }
            }
            GetItemVal();
        }

        private void GetItemVal()
        {
            ItemVal.Text = "[not selected]";
            if (Grid.SelectedItems != null)
            {
                if (Grid.SelectedItems.Count > 0)
                {
                    if (AllColumns.SelectedItem != null)
                    {
                        try
                        {
                            ItemVal.Text =
                                ((DataRows) Grid.SelectedItems[0].Tag).FieldByName(
                                    AllColumns.SelectedItem.ToString().ToUpper());
                        }
                        catch (ArgumentException)
                        {
                            ItemVal.Text = "<No data>";
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!columns.Contains(newColomn.Text.ToUpper()))
            {
                columns.Add(newColomn.Text.ToUpper());
            }
            SetupControls();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (AllColumns.SelectedItem != null)
            {
                columns.Remove(AllColumns.SelectedItem.ToString());
            }
            SetupControls();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            AllRows.Add(new DataRows());
            SetupControls();
        }

        private void Grid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetItemVal();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (Grid.SelectedItems != null)
                if (Grid.SelectedItems.Count > 0)
                    AllRows.Remove((DataRows) Grid.SelectedItems[0].Tag);
            SetupControls();
        }

        private void AllColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetItemVal();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (Grid.SelectedItems != null)
            {
                if (Grid.SelectedItems.Count > 0)
                {
                    if (AllColumns.SelectedItem != null)
                    {
                        try
                        {
                            ((DataRows) Grid.SelectedItems[0].Tag).FieldByName(
                                AllColumns.SelectedItem.ToString().ToUpper());
                            ((DataRows) Grid.SelectedItems[0].Tag).DeleteField(
                                AllColumns.SelectedItem.ToString().ToUpper());
                        }
                        catch (ArgumentException)
                        {
                        }
                        ((DataRows) Grid.SelectedItems[0].Tag).AddField(
                            new DataField(AllColumns.SelectedItem.ToString().ToUpper(), ItemVal.Text));
                        // save pos
                        int Cpos = AllColumns.SelectedIndex;
                        int Gpos = Grid.SelectedIndices[0];
                        SetupControls();
                        // set pos
                        AllColumns.SelectedIndex = Cpos;
                        Grid.Items[Gpos].Selected = true;
                    }
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            columns.Clear();
            AllRows.Clear();
            AllRows = XmlQueryLoader.XMLStringToRows(XmlText.Text);
            if(AllRows.Count > 0)
            {
                foreach (DataRows row in AllRows)
                {
                    
                    foreach (DataField field in row.fields)
                    {
                        if (!columns.Contains(field.Field))
                            columns.Add(field.Field);
                    }
                }

            }
            SetupControls();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            XmlText.Text = XmlQueryLoader.RowsToXMLString(AllRows);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string _Text = XmlText.Text;
            _Text = _Text.Replace("\\", "\\\\");
            _Text = _Text.Replace("\"", "\\\"");
            _Text = _Text.Replace("\n", "");
            _Text = _Text.Replace("\r", "");
            _Text = "string Xml = \"" + _Text + "\";";
            _Text = "#region XmlText\n/*\n" + XmlText.Text + "\n*/\n#endregion\n" + _Text;
            Clipboard.SetData(DataFormats.StringFormat, _Text);
        }
    }
} 